<?php
namespace App\Http\Controllers\Payment;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Order\OrderDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Libraries\Tools\ConstVar;
use App\Models\Order\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;


class OrderController extends Controller{


    private $order;
    private $isEndDecorator;
    private $coverDecorator;
    private $orderDecorator;

    public function __construct(Order $order, IsEndDecorator $isEndDecorator,
                                CoverDecorator $coverDecorator, OrderDecorator $orderDecorator)
    {
        $this->order = $order;
        $this->isEndDecorator = $isEndDecorator;
        $this->coverDecorator = $coverDecorator;
        $this->orderDecorator = $orderDecorator;
    }

    /**
     * 订单列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function orders(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
            'status' => 'integer|in:0,1,2,3,5,6',
            'type' => 'integer|in:1,2,3,4',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'status.integer' => '10002',
            'status.in' => '10002',
            'type.integer' => '10002',
            'type.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->order->addDecorator($this->orderDecorator);
        $this->order->addDecorator($this->isEndDecorator);
        $data = $this->order->orders(
            $request['page'],
            $request['size'],
            Auth::id(),
            $request['status']??null,
            $request['type']??null
        );
        return $this->sendSuccessResponse($data);
    }

    /**
     * 修改
     * @param $id
     * @param Request $request
     * @param Order $order
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit($id, Request $request, Order $order){
        $valid_data = $request->all();
        $valid_data['id'] = $id;
        $validator = Validator::make($valid_data, [
            'id' => 'required|integer',
            'status' => 'required|integer|in:7', //7-取消
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
            'status.required' => '10001',
            'status.integer' => '10002',
            'status.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }

        $order = $order->where('id', $id)->first();
        if(empty($order)){
            return $this->sendFailResponse(400, 40400);
        }
        if($order->user_id != Auth::id()){
            return $this->sendFailResponse(400, 40101);
        }
        if($order->status != ConstVar::$ORDER_STATUS_WAITING){
            return $this->sendFailResponse(400, 40001);
        }
        $order->status = ConstVar::$ORDER_STATUS_CANCEL;
        $order->save();
        return $this->sendSuccessResponse([]);
    }
}